import { Button, Toggle } from "cc";
import { inner } from "../../core/boot";
import { __cfw_notifications__ } from "../../configs";

const origin = Button.prototype["_onTouchEnded"];
const { Audio: { DEFAULT_BUTTON, DEFAULT_TOGGLE } } = __cfw_notifications__;

Object.defineProperty(Button.prototype, '_onTouchEnded', {
    configurable: true, enumerable: false,
    value: function (...args: any[]) {
        const ntf = this instanceof Toggle ? DEFAULT_TOGGLE : DEFAULT_BUTTON;
        // 同时播放特定音效和有默认音效时，先执行的原方法覆盖特定音效。
        Function.invoke(origin, this, ...args);
        const arr = this.node.name.split(':');
        // 指定:none可以移除默认音效
        if (arr.length > 1) {
            inner.dispatch(ntf, arr.tail === '' ? 'none' : arr.tail);
        } else inner.dispatch(ntf);
    },
});